<?php

namespace App\Http\Controllers\Api\V1\Mes;

use App\Helpers\ResponseHelper;
use App\Http\Controllers\Controller;
use App\Models\ERP\Customer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CustomerController extends Controller
{
    /*
     * 列表
     * BP_CLUSTER_ID 群组ID 桥梁ID为8F3B5EC8-1A79-4382-40E1-16E5D98BE249
     */
    public function list(Request $request) :JsonResponse
    {
        $result = Customer::query()
            ->where("BP_CLUSTER_ID", '8F3B5EC8-1A79-4382-40E1-16E5D98BE249')
            ->when($request, function ($query) use ($request) {
                if ($request->input("name")) {
                    $query->where("CUSTOMER_FULL_NAME", "like", "%" . $request->input("name") . "%");
                }
                return $query;
            })
            ->paginate($request->input("size"));
        $list = $result->getCollection()->map(function ($item) {
            return $item->only(["CUSTOMER_CODE", "CUSTOMER_NAME", "CUSTOMER_FULL_NAME"]);
        });
        $total = $result->total();
        $return = compact("list", "total");
        return ResponseHelper::json(0, "success", $return);
    }
}
